package cn.com.sbell.vss.csg.vap;

import java.util.ArrayList;
import java.util.List;

import org.dom4j.Element;

import cn.com.sbell.vss.VssConstants;
import cn.com.sbell.vss.VssException;
import cn.com.sbell.vss.csg.vap.bean.CameraHide;
import cn.com.sbell.vss.csg.vsclient.VsClient;
import cn.com.sbell.vss.csg.vsclient.util.PropertiesHolder;

public class SetHideImageArea extends ProtectedVsCommander {

	protected Element executeCommand(VsClient vsClient,
			PropertiesHolder holder, Element root) {
		CameraHide cameraHide = getHideImageAreas(holder);
		if (!vsClient.setHideImageArea(cameraHide)) {
			throw new VssException(VssConstants.CODE_SET_HIDE_IMAGE_AREA_FAILED);
		}
		return root;
	}
	
	private CameraHide getHideImageAreas(PropertiesHolder holder) {
		Integer cameraNumber = holder.getIntProperty("cameraNumber");
		String[] hideImageAreas = holder.getStrProperties("hideImageArea");
		if (cameraNumber == null) 
			throw new VssException(VssConstants.CODE_REQUIRED,
					new String[] { "csg.cameraNumber"  });
		
		CameraHide cameraHideImage = new CameraHide();
		cameraHideImage.setCameraNumber(cameraNumber);
		
		List temp = new ArrayList();
		if (hideImageAreas != null) {
			for (int i = 0; i < hideImageAreas.length; i++) {
				String[] datas = hideImageAreas[i].split(",");
				if (datas.length == 4) {
					CameraHide.HideImageArea hideImageArea = cameraHideImage.new HideImageArea();	
					hideImageArea.setX(Integer.valueOf(datas[0]));
					hideImageArea.setY(Integer.valueOf(datas[1]));
					hideImageArea.setWidth(Integer.valueOf(datas[2]));
					hideImageArea.setHeight(Integer.valueOf(datas[3]));
					temp.add(hideImageArea);
				} 
			}
		}
		cameraHideImage.setHideImageAreas(
				(CameraHide.HideImageArea[]) temp.toArray(new CameraHide.HideImageArea[0]));
		
		return cameraHideImage;
	}

}
